FOR

Syntax: FOR var = range *[,rangei]*
Location: QL ROM

The SuperBASIC version of the classic FOR loop is extremely flexible. The syntax of this SuperBASIC structure can take two forms: 

FOR var=range *[,rangei]* :statement *[:statement]*

or   

FOR var=range *[,rangei]*
  *[statements]*
[EXIT var]
[NEXT var]
END FOR var

where range can be one of the following:   

start_value TO end_value [STEP step] ,or   
value 

The first of these variants is known as an in-line FOR loop. Provided that there is at least one statement following FOR, this line will be repeated until the end value is reached.  There is no need for a related END FOR statement and therefore the shortest in-line FOR loop possible is:

FOR x=1 to 100:NEXT x

If an in-line loop is terminated prematurely, for example with EXIT, control will be passed to the statement following the corresponding END FOR statement (if one exists), or the next program line.  This allows the following:       

FOR x=1 TO 100: IF INKEY$=' ': EXIT x: END FOR x: PRINT x

The basic function of FOR is to count a floating point variable from a given start value to an end value by adding step to var during each pass of the loop (step may be positive or negative depending on the start and end values). If no step is specified, STEP 1 will be assumed. However, if step is negative when the end value is greater than the start value (or vice versa), then the loop will immediately exit, and nothing contained in the loop will be processed.

A similar effect can be achieved by using a REPeat structure:   

var=start_value
REPeat loop
...
IF var>=end_value THEN EXIT loop: ELSE var=var+step 
END REPeat loop


The similarity between these two SuperBASIC loop types can be extended to the use of EXIT and NEXT statements which can be used identically in both structures.  EXIT terminates the loop, and the next statement which will be processed is the first statement after the corresponding END FOR.  NEXT forces the program to make the next pass of the loop.

PROGRAMMING NOTES
(1) When NEXT is used within a FOR..END FOR structure, if var is already at the end_value, the NEXT statement will have no effect:   

100 FOR x=1 TO 9
110   PRINT x;"   ";
120   IF x MOD 2 THEN NEXT x
130   PRINT x^2
140 END FOR x

Output: 1   2   43   4   165   6   367   8   649   81

To prevent the odd result when x=9, line 120 would need to be altered to read:   

120   IF x MOD 2 THEN NEXT x: EXIT x

(2) Except on a Minerva ROM or under SMS, the loop variable is set to 0 before the FOR is executed, therefore the following program prints the square roots of the numbers 0 to 9:   

100 x=3   
110 FOR x=x TO 9   
120   PRINT x;'   ';   
130   IF NOT RND(10) THEN EXIT x   
140   PRINT SQRT(x)   
150 END FOR x

On Minerva ROMs and under SMS, this would print out all of the square roots of the numbers 3 to 9 (as expected).

(3) Single values and intervals can be freely mixed after the equals sign: the following examples are valid expressions:

FOR x=2,4 TO 10 STEP 2,4.5,7 TO -4 STEP -.2   
FOR x=1

Example:
A short routine to count the lines of a text file (using the oddities of the NEXT command):   

100 OPEN#3,file   
110 FOR lines=0 TO 10000   
120   IF EOF(#3) THEN PRINT lines: EXIT lines   
130   INPUT #3,line$: NEXT lines   
140   PRINT 'OOPS - program is longer than 10000 lines!!'   
150 END FOR lines   
160 CLOSE#3

MINERVA NOTES:
On a Minerva machine, a FOR loop can use either a single character string variable or an integer variable:   

FOR A$='A' TO 'Z' STEP CHR$(2):PRINT A$;' ';

This prints out A C E G I K M O Q S U W Y.   

FOR loop%=1 TO 255   

This is a little quicker than FOR loop=1 to 255


SMS NOTES:
Like Minerva, SMS will allow you to use integer variables in FOR loops (but not string variables).  As from v2.57, the range is checked to ensure that it is within the valid word integer range (-327678..32767) when the FOR loop is started, otherwise it returns 'Error in Expression'.

EXIT, NEXT and END FOR do not need to contain the loop identifier, SMS will presume that they refer to the loop currently being executed.

CROSS-REFERENCE:
REPeat...END REPeat is the other loop type.
